// 存储工具函数

/**
 * 设置localStorage
 * @param {string} key 键名
 * @param {any} value 值
 * @param {number} expire 过期时间(毫秒)
 */
export function setLocalStorage(key, value, expire = null) {
  try {
    const data = {
      value,
      expire: expire ? Date.now() + expire : null
    }
    localStorage.setItem(key, JSON.stringify(data))
  } catch (error) {
    console.error('设置localStorage失败:', error)
  }
}

/**
 * 获取localStorage
 * @param {string} key 键名
 * @param {any} defaultValue 默认值
 * @returns {any} 存储的值
 */
export function getLocalStorage(key, defaultValue = null) {
  try {
    const data = localStorage.getItem(key)
    if (!data) return defaultValue

    const parsed = JSON.parse(data)

    // 检查是否过期
    if (parsed.expire && Date.now() > parsed.expire) {
      localStorage.removeItem(key)
      return defaultValue
    }

    return parsed.value
  } catch (error) {
    console.error('获取localStorage失败:', error)
    return defaultValue
  }
}

/**
 * 删除localStorage
 * @param {string} key 键名
 */
export function removeLocalStorage(key) {
  try {
    localStorage.removeItem(key)
  } catch (error) {
    console.error('删除localStorage失败:', error)
  }
}

/**
 * 清空localStorage
 */
export function clearLocalStorage() {
  try {
    localStorage.clear()
  } catch (error) {
    console.error('清空localStorage失败:', error)
  }
}

/**
 * 设置sessionStorage
 * @param {string} key 键名
 * @param {any} value 值
 */
export function setSessionStorage(key, value) {
  try {
    sessionStorage.setItem(key, JSON.stringify(value))
  } catch (error) {
    console.error('设置sessionStorage失败:', error)
  }
}

/**
 * 获取sessionStorage
 * @param {string} key 键名
 * @param {any} defaultValue 默认值
 * @returns {any} 存储的值
 */
export function getSessionStorage(key, defaultValue = null) {
  try {
    const data = sessionStorage.getItem(key)
    return data ? JSON.parse(data) : defaultValue
  } catch (error) {
    console.error('获取sessionStorage失败:', error)
    return defaultValue
  }
}

/**
 * 删除sessionStorage
 * @param {string} key 键名
 */
export function removeSessionStorage(key) {
  try {
    sessionStorage.removeItem(key)
  } catch (error) {
    console.error('删除sessionStorage失败:', error)
  }
}

/**
 * 清空sessionStorage
 */
export function clearSessionStorage() {
  try {
    sessionStorage.clear()
  } catch (error) {
    console.error('清空sessionStorage失败:', error)
  }
}

/**
 * 检查localStorage是否可用
 * @returns {boolean} 是否可用
 */
export function isLocalStorageAvailable() {
  try {
    const test = '__localStorage_test__'
    localStorage.setItem(test, test)
    localStorage.removeItem(test)
    return true
  } catch {
    return false
  }
}

/**
 * 检查sessionStorage是否可用
 * @returns {boolean} 是否可用
 */
export function isSessionStorageAvailable() {
  try {
    const test = '__sessionStorage_test__'
    sessionStorage.setItem(test, test)
    sessionStorage.removeItem(test)
    return true
  } catch {
    return false
  }
}

/**
 * 获取存储大小(字节)
 * @param {string} type 存储类型 'local' | 'session'
 * @returns {number} 存储大小
 */
export function getStorageSize(type = 'local') {
  try {
    const storage = type === 'local' ? localStorage : sessionStorage
    let total = 0

    for (const key in storage) {
      if (storage.hasOwnProperty(key)) {
        total += storage[key].length + key.length
      }
    }

    return total
  } catch (error) {
    console.error('获取存储大小失败:', error)
    return 0
  }
}

/**
 * 获取存储使用情况
 * @returns {object} 存储使用情况
 */
export function getStorageUsage() {
  return {
    local: {
      available: isLocalStorageAvailable(),
      size: getStorageSize('local'),
      sizeFormatted: formatFileSize(getStorageSize('local'))
    },
    session: {
      available: isSessionStorageAvailable(),
      size: getStorageSize('session'),
      sizeFormatted: formatFileSize(getStorageSize('session'))
    }
  }
}

// 导入格式化函数
import { formatFileSize } from './format'
